package org.company.saturn.hadoop.yarn.reduce;

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

/**
 * 通过reduce统计每个单词出现的次数
 * @author hucw
 *
 */
public class WordReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
	
	private IntWritable result = new IntWritable(0);
	
	@Override
	protected void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {
		Iterator<IntWritable> iter = values.iterator();
		int sum = 0;
		while(iter.hasNext()) {
			sum += iter.next().get();
		}
		this.result.set(sum);
		context.write(key, this.result);
	}
	
}
